<template>
    <el-row :gutter="20">
        <el-col :span="8" style="margin-left: 10px;">
            <sy-baselist
                    ref="moduleList"
                    title="模块"
                    :hidePagination="true"
                    :hideHeader="true"
                    @current-change="onModuleChange"
                    editVue="./system/Module_Edit.vue"
                    listUrl="server/system/Module.ashx?cmd=GetList"
                    deleteUrl="server/system/Module.ashx?cmd=Delete"
            >
                <el-table-column prop="FID" label="ID" width="30" sortable>
                </el-table-column>
                <el-table-column prop="FName" label="模块名称" width="120" sortable>
                </el-table-column>
            </sy-baselist>
        </el-col>

        <el-col :span="14" class="toolbar" style="margin-left: 50px;">
            <el-form :inline="true">
                <el-form-item>
                    <el-tag type="gray">菜单</el-tag>
                </el-form-item>
                <el-form-item>
                    <el-button  type="primary" size="small" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <el-col :span="14" style="margin-left: 50px;">
            <el-tree
                    ref="productType"
                    v-loading="treeLoading"
                    :data="data2"
                    :props="defaultProps"
                    node-key="FID"
                    highlight-current
                    :render-content="renderContent">
            </el-tree>
            <br>
            <br>
            <el-dialog title="菜单" size="tiny" v-model="treeFormVisible" top="5%" :close-on-click-modal="true" :modal="true">
                <el-form :model="treeForm" label-width="110px">
                    <el-form-item label="名称">
                        <el-input v-model="treeForm.FName"></el-input>
                    </el-form-item>
                    <el-form-item label="顺序">
                        <el-input v-model="treeForm.FOrder"></el-input>
                    </el-form-item>
                    <el-form-item label="图标">
                        <el-input v-model="treeForm.FIconCls_Vue"></el-input>
                    </el-form-item>
                    <el-form-item label="Path">
                        <el-input v-model="treeForm.FUrl_Vue"></el-input>
                    </el-form-item>
                    <el-form-item label="Require">
                        <el-input v-model="treeForm.FRequire_Vue"></el-input>
                    </el-form-item>
                    <el-form-item label="Query">
                        <el-input v-model="treeForm.FQuery"></el-input>
                    </el-form-item>
                    <el-form-item label="是否显示">
                        <el-checkbox v-model="treeForm.FVisable_Vue"></el-checkbox>
                    </el-form-item>
                    <el-form-item label="父ID">
                        <el-input v-model="treeForm.FParent"></el-input>
                    </el-form-item>
                    <el-form-item label="模块ID" v-show="treeForm.FLevel==1">
                        <el-input v-model="treeForm.FModuleID"></el-input>
                    </el-form-item>
                    <el-form-item label="">
                        <el-tag>菜单ID:{{treeForm.FID}}</el-tag>
                        <el-tag type="danger">注：菜单只支持2层，第三层为权限项</el-tag>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="treeFormVisible = false">取 消</el-button>
                    <el-button type="primary" :loading="formLoading" @click="saveTree">确 定</el-button>
                </div>
            </el-dialog>
        </el-col>
    </el-row>
</template>

<script>
    import util from '../../common/util'
    export default {
        data() {
            return {
                fixWhere:"",
                store:null,
                data2: [],
                selectNodeData:{},
                defaultProps: {
                    children: 'children',
                    label: 'FName'
                },
                treeForm:{},
                treeFormVisible:false,
                treeLoading:false,
                formLoading:false,
                ModuleID:0
            }
        },
        methods: {
            switchLoading(flag){
                this.treeLoading = flag;
            },
            switchFormLoading(flag){
                this.formLoading = flag;
            },
            handleAdd:function(){
                this.treeFormVisible = true
                this.selectNodeData = null;
                this.treeForm = {id:0,FID:0,FParent:0,FVisable_Vue:true,FLevel:1,FModuleID:this.ModuleID,
                    FUrl_Vue:"/",FRequire_Vue:"Home.vue",FIconCls_Vue:"el-icon-message"}
                this.store = null;
            },
            saveTree(){
                util.post("server/system/Menu.ashx?cmd=SaveMenu",this.treeForm,this.switchFormLoading,result=> {
                    util.msgSuc("保存成功")
                    this.treeFormVisible = false
                    if(this.treeForm.FID==0){
                        this.treeForm.FID = result.FID
                        if(this.store) {
                           // this.store.append(Object.assign({children: []}, this.treeForm), this.selectNodeData)
                            this.refrash()
                        }else{
                            //刷新
                            this.refrash()
                        }
                    }else{
                        if(this.selectNodeData) {
                           // this.selectNodeData = Object.assign(this.selectNodeData, this.treeForm)
                            this.refrash()
                        }else{
                            this.refrash()
                        }
                    }
                })
            },
            append(store, data,node) {
                this.treeFormVisible = true
                this.selectNodeData = data;
                this.treeForm = {id:0,FID:0,FParent:data.FID,FVisable_Vue:true,FModuleID:this.ModuleID};
                //如果是权限项
                if(data.FUrl_Vue=="/"){
                    this.treeForm.FLevel = 2
                }else{
                    this.treeForm.FUrl_Vue ="#"
                    this.treeForm.FLevel = 3
                }
                this.store = store;
                //store.append({ id: 1000, label: 'testtest', children: [] }, data);
            },
            edit(store, data,node) {
                this.treeFormVisible = true
                this.selectNodeData = data
                this.treeForm = Object.assign({},data)
                this.store = store
                //store.append({ id: 1000, label: 'testtest', children: [] }, data);
            },
            remove(store, data,node) {
                // TO-DO: 需要提示是否删除
                let url = "server/system/Menu.ashx?cmd=DeleteMenu"
                util.post(url, {FID:data.FID},this.switchLoading,result=> {
                    util.msgSuc("删除成功")
                    store.remove(data);
                })
            },
            renderContent(h, { node, data, store }) {
                return (
                    <span>
                    <span>
                    <span>{node.label}</span>
                </span>
                <span style="float: right; margin-right: 20px">
                    <el-button size="mini" on-click={ () => {this.append(store, data,node);event.stopPropagation()}}>添加下级</el-button>
                <el-button size="mini" on-click={ () =>{this.edit(store, data,node);event.stopPropagation()} }>修改</el-button>
                <el-button size="mini" type="danger" on-click={ () => {this.remove(store, data,node);event.stopPropagation()} }>删除</el-button>
                </span>
                </span>);
            },
            refrash(){
                util.get("server/system/Menu.ashx?cmd=GetMenuTree",{FModuleID: this.ModuleID},this.switchLoading,result=> {
                    this.data2 = result.data;
                });
            },
            onModuleChange(row,oldRow){
                if(row) {
                    this.ModuleID = row.FID;
                    this.refrash();
                }
           }
        },
        mounted() {
            this.refrash()
        }
    };
</script>